package org.dreamfly.core.dao.impl;

import org.dreamfly.core.bean.SzUser;
import org.dreamfly.core.bean.SzUsers;
import org.dreamfly.core.dao.UserDAO;
import org.hibernate.Criteria;

import java.util.ArrayList;
import java.util.List;

import static org.hibernate.criterion.Restrictions.eq;
import static org.hibernate.criterion.Restrictions.not;

/**
 * Created by IntelliJ IDEA.
 * User: gin
 * Date: 8/12/11
 * Time: 10:49 AM
 * To change this template use File | Settings | File Templates.
 */
public class UserDAOImpl extends BaseDAOImpl<SzUsers, Long> implements UserDAO {
    @Override
    public List<SzUsers> findAllNative(Class clazz, String server, boolean isNew) {
        Criteria criteria=this.getSession().createCriteria(clazz);
        criteria.add(eq("server",server));
        criteria.add(eq("isNew",isNew));
        criteria.add(not(eq("isAdmin",true)));
        List result = criteria.list();
        return (!((result == null) || result.isEmpty())) ? result : new ArrayList<SzUser>();

    }

    @Override
    public SzUsers findByName(String server, String username) {
        Criteria criteria = this.getSession().createCriteria(SzUsers.class);
        criteria.add(eq("server",server));
        criteria.add(eq("username",username));
        List result = criteria.list();
        return (!((result == null) || result.isEmpty())) ? (SzUsers) result.get(0) : null;  //TODO
    }

    @Override
    public void updateAuthority(SzUser user) {
        //TODO
    }

    @Override
    public SzUsers findAdmin(String server) {
        Criteria criteria = this.getSession().createCriteria(SzUsers.class);
        criteria.add(eq("server",server));
        criteria.add(eq("isAdmin",true));
        List result = criteria.list();
        return (!((result == null) || result.isEmpty())) ? (SzUsers) result.get(0) : null;
    }
}
